class Edition < ActiveRecord::Base
  #Relations
  belongs_to	:release
  belongs_to	:label
  has_many	:images, :as => :photographable, :dependent => :destroy
  has_many	:edits, :as => :editable, :order => "created_at DESC"
  has_many	:editions_genres
  has_many	:genres, :through => :editions_genres, :source => :genre
  has_many	:edition_credits
  has_many	:edition_dates
  has_many  :discs, :order => "number"

  #Virtual Attributes
  accepts_nested_attributes_for :release

  #Callbacks
  after_update :create_edit
  after_create :new_entry_edit

  #Constants
  EDITION_STATUSES = %w(Recording Mastering Released)
  EDITION_FORMATS = %w(CD DVD Vinyl Cassette Digitial)
  
  #
  #
  def to_s
    return title
  end

  #
  def to_param
    "#{id}-#{title.parameterize}"
  end

  #
  def primary_image
    if images.empty?
      return nil
    else
      images.first.image
    end
  end

  private

  #
  def new_entry_edit
    entry = Hash.new
    entry.store("created", [self.title])
    Edit.create_edit(entry, self)
  end

  #
  def create_edit
    if self.changed?
      puts changes
      Edit.create_edit(self.changes, self)
    end
  end
end
